package com.nidu.demo.notice.gatewayimpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.dto.PageQuery;
import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nidu.demo.notice.convertor.NoticeConvertor;
import com.nidu.demo.notice.gateway.NoticeGateway;
import com.nidu.demo.notice.mapper.NoticeMapper;
import com.nidu.demo.notice.mapper.dataobject.NoticeDO;
import com.nidu.demo.notice.model.Notice;
import com.nidu.demo.notice.model.NoticeCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 通知公告网关实现
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class NoticeGatewayImpl implements NoticeGateway {

    private final NoticeMapper mapper;
    private final NoticeConvertor convertor;

    @Override
    public Notice create(Notice notice) {
        NoticeDO noticeDO = convertor.toDataObject(notice);
        mapper.insert(noticeDO);
        return convertor.toDomainObject(noticeDO);
    }

    @Override
    public Notice update(Notice notice) {
        NoticeDO noticeDO = convertor.toDataObject(notice);
        mapper.updateById(noticeDO);
        return convertor.toDomainObject(noticeDO);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteById(id);
    }

    @Override
    public void deleteByIds(Set<Long> ids) {
        mapper.deleteByIds(ids);
    }

    @Override
    public Notice getById(Long id) {
        NoticeDO noticeDO = mapper.selectById(id);
        if (noticeDO == null) {
            return null;
        }
        return convertor.toDomainObject(noticeDO);
    }

    @Override
    public List<Notice> getByIds(Set<Long> ids) {
        LambdaQueryWrapper<NoticeDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(NoticeDO::getId, ids);
        List<NoticeDO> noticeDOS = mapper.selectList(wrapper);
        if (CollUtil.isEmpty(noticeDOS)) {
            return new ArrayList<>();
        }
        return convertor.toDomainObjects(noticeDOS);
    }

    @Override
    public List<Notice> listByCondition(NoticeCriteria criteria) {
        LambdaQueryWrapper<NoticeDO> wrapper = buildQueryWrapper(criteria);
        List<NoticeDO> noticeDOS = mapper.selectList(wrapper);
        if (CollUtil.isEmpty(noticeDOS)) {
            return new ArrayList<>();
        }
        return convertor.toDomainObjects(noticeDOS);
    }

    @Override
    public PageResponse<Notice> pageByCondition(NoticeCriteria criteria) {
        // 创建查询条件
        LambdaQueryWrapper<NoticeDO> wrapper = buildQueryWrapper(criteria);

        // 创建分页对象
        Page<NoticeDO> page = new Page<>(criteria.getPageIndex(), criteria.getPageSize());

        // 排序字段
        if (!StrUtil.isEmpty(criteria.getOrderBy())) {
            if (PageQuery.DESC.equals(criteria.getOrderDirection())) {
                page.addOrder(OrderItem.desc(criteria.getOrderBy()));
            } else {
                page.addOrder(OrderItem.asc(criteria.getOrderBy()));
            }
        }

        // 执行分页查询
        mapper.selectPage(page, wrapper);
        List<Notice> notices = convertor.toDomainObjects(page.getRecords());
        return PageResponse.of(notices, (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        LambdaUpdateWrapper<NoticeDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(NoticeDO::getId, id);
        wrapper.set(NoticeDO::getStatus, status);
        mapper.update(null, wrapper);
    }

    private LambdaQueryWrapper<NoticeDO> buildQueryWrapper(NoticeCriteria criteria) {
        LambdaQueryWrapper<NoticeDO> wrapper = new LambdaQueryWrapper<>();
        
        // 状态条件
        wrapper.eq(criteria.getStatus() != null, NoticeDO::getStatus, criteria.getStatus());
        
        // 时间范围条件
        LocalDateTime[] createTime = criteria.getCreateTime();
        if (createTime != null) {
            if (createTime.length == 1 && createTime[0] != null) {
                wrapper.ge(NoticeDO::getCreateTime, createTime[0]);
            } else if (createTime.length > 1 && createTime[0] != null && createTime[1] != null) {
                wrapper.between(NoticeDO::getCreateTime, createTime[0], createTime[1]);
            }
        }
        
        // 精确查询条件
        wrapper.eq(criteria.getType() != null, NoticeDO::getType, criteria.getType());
        
        // 模糊查询条件
        wrapper.like(!StrUtil.isEmpty(criteria.getTitle()), NoticeDO::getTitle, criteria.getTitle());
        
        return wrapper;
    }

}
